<template>
  <el-dialog title="编辑地址" :visible.sync="dialogVisible" @close="$emit('update:visible',false)"
             :close-on-click-modal="handleClose" append-to-body>
    <div class="dialog_conwrap">
      <el-form ref="form" :model="form" label-width="90px">
        <el-form-item label="地址搜索:">
          <div style="display: flex;align-items: center;">
            <el-input v-model="form.company_address" placeholder="详细地址" size="small"></el-input>
            <el-button size="small" @click="filterkeyword" style="margin-left: 10px;">查询</el-button>
          </div>
        </el-form-item>
        <el-form-item label="地图定位:">
          <div id="container" style="width:100%;height:500px;">
            <div class="containerMap"><img src="/static/img/centerAdress.png" alt=""></div>
          </div>
          *只用于获取位置坐标，当前坐标：{{ `${this.mapInfo.longitude},${this.mapInfo.latitude}` }}
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false" size="small">取 消</el-button>
      <el-button type="primary" @click="onSubmit" size="small">确定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import adressMap from '@/assets/images/adressMap.png'

export default {
  name: "enterpriseAddressDialog",
  props: ["visible", "formData"],
  data() {
    return {
      adressMap: adressMap,
      dialogVisible: false,
      form: {
        // location: "24.491234,118.194489",
        location: "30.670795844883525,104.06780609210014", // 104.187249,30.437739
        company_address: ''
      },
      options: [],
      handleClose: false,
      mapSearchType: 1, // 1 逆解析 2 地址解析
      mapInfo: {
        longitude: '',//经度
        latitude: '',//纬度
        city: '',
        zoom: 15,
        map: null, // 初始化地图
        geocoder: null, // 地址搜索
        marker: null // 标注提示
      }
    };
  },
  mounted() {

  },
  watch: {
    visible() {
      let n = this;
      this.dialogVisible = this.visible;
      if (this.visible) {
        if (this.formData.latitude && this.formData.longitude) {
          this.form.location = `${this.formData.latitude},${this.formData.longitude}`;
        }
        this.$nextTick(function () {
          n.getMyLocation()
        })
      } else {
        this.form = {
          location: "30.670795844883525,104.06780609210014", // 104.187249,30.437739
          company_address: ''
        }
      }
    }
  },
  created() {
  },
  methods: {
    onSubmit() {
      // if (!this.form.company_address) {
      //   this.$message.error('请填写公司地址');
      //   return
      // }
      this.$emit('editFrom', {map: this.mapInfo, company_address: this.form.company_address});
    },
    getMyLocation() {
      console.log('判断坐标地址', this.form)
      if (!this.form.location) {
        let geolocation = new qq.maps.Geolocation("BN7BZ-EA3WW-BSVR6-RZ26N-27YAZ-NGBDW", "地图地址");
        geolocation.getIpLocation(this.showPosition, this.showErr);
      } else {
        // let location = this.form.location.split(',')
        // 24.476853 118.094377
        this.mapInfo.latitude = this.form.location.split(',')[1];
        this.mapInfo.longitude = this.form.location.split(',')[0];
        // 24.49123351343934 118.19448911037632
        console.log('获取保存的经纬度：', this.mapInfo)
        this.init();
      }
    },
    //成功获取地址
    showPosition(position) {
      console.log('进入获取当前地址：', position);
      this.mapInfo.latitude = position.lng;
      this.mapInfo.longitude = position.lat;
      this.init();
    },
    // 获取地址失败
    showErr() {
      console.log("定位失败");
      this.getMyLocation();//定位失败再请求定位，测试使用
    },
    init() {
      //步骤：定义map变量 调用 qq.maps.Map() 构造函数 获取地图显示容器
      // 设置地图中心点
      console.log('地图变换：', this.mapInfo.longitude, this.mapInfo.latitude)
      console.log(this.mapSearchType)
      let this_ = this;
      let myLatlng = new qq.maps.LatLng(this.mapInfo.longitude, this.mapInfo.latitude); //定义工厂模式函数
      let myOptions = {
        zoom: this.mapInfo.zoom, //设置地图缩放级别
        center: myLatlng, //设置中心点样式
        mapTypeControl: false,
        panControl: false,
        zoomControl: false
      }

      // 获取dom元素添加地图信息
      this.mapInfo.map = new qq.maps.Map(document.getElementById("container"), myOptions);
      //给定位的位置添加图片标注
      this.mapInfo.marker = new qq.maps.Marker({
        position: myLatlng,
        map: this.mapInfo.map
      });
      // let anchor = new qq.maps.Point(0, 39);
      let size = new qq.maps.Size(40, 40);
      let origin = new qq.maps.Point(0, 0);
      let icon = new qq.maps.MarkerImage(
        this.adressMap,
        size,
        origin
      );
      this.mapInfo.marker.setIcon(icon);
      this.mapInfo.geocoder = new qq.maps.Geocoder({
        complete: function (result) {
          console.log('result:', result)
          this_.form.company_address = result.detail.address
          this_.form.location = result.detail.location.lat + ',' + result.detail.location.lng
          if (this_.mapSearchType === 2) {
            console.log('你解析', result)
            this_.LatLngMap(result.detail.location.lat, result.detail.location.lng);
          }

        }
      });
      //当地图中心属性更改时触发事件
      qq.maps.event.addListener(this_.mapInfo.map, 'center_changed', function () {
        this_.mapInfo.latitude = this_.mapInfo.map.getCenter().lat;
        this_.mapInfo.longitude = this_.mapInfo.map.getCenter().lng;
      });
      // 拖动停止
      qq.maps.event.addListener(this_.mapInfo.map, 'dragend', function () {
        console.log('停止拖动数据点：')
        this_.mapSearchType = 1;
        this_.LatLngMap(this_.mapInfo.latitude, this_.mapInfo.longitude)
      });
    },
    LatLngMap(lat, lng) {
      console.log(lat, lng)
      this.form.location = lat + ',' + lng;
      let latLng = new qq.maps.LatLng(lat, lng);
      let this_ = this;
      if (this.mapSearchType === 2) {
        // let myOptions = {
        //   zoom: this.mapInfo.zoom, //设置地图缩放级别
        //   center: latLng, //设置中心点样式
        //   mapTypeControl: false,
        //   panControl: false,
        //   zoomControl: false
        // }
        // 获取dom元素添加地图信息
        // this.mapInfo.map = new qq.maps.Map(document.getElementById("container"), myOptions);
        this.mapInfo.map.setCenter(latLng)
      }
      this.mapInfo.marker.setMap(null);
      this.mapInfo.marker = new qq.maps.Marker({
        position: latLng,
        map: this_.mapInfo.map
      });
      let size = new qq.maps.Size(40, 40);
      let origin = new qq.maps.Point(0, 0);
      let icon = new qq.maps.MarkerImage(
        this.adressMap,
        size,
        origin
      );
      this.mapInfo.marker.setIcon(icon);
      if (this.mapSearchType === 1) {
        console.log('123')
        this.mapInfo.geocoder.getAddress(latLng);
      }
    },
    filterkeyword() {
      this.mapSearchType = 2; // 1 逆解析 2 地址解析
      if (this.form.company_address) {
        this.mapInfo.geocoder.getLocation(this.form.company_address);
      } else {
        var geolocation = new qq.maps.Geolocation("BN7BZ-EA3WW-BSVR6-RZ26N-27YAZ-NGBDW", "地图地址");
        geolocation.getIpLocation(this.showPosition, this.showErr);
      }
    }
  },

};
</script>
